package com.yonyou.pmclouds.safetypatrol.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.picture.entity.PictureVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.safetypatrol.entity.ParentState;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolDetailMapper;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolMapper;
import com.yonyou.pmclouds.safetypatrol.process.SafetypatrolDeleteCheckProcess;
import com.yonyou.pmclouds.safetypatrol.process.SafetypatrolInsertCheckProcess;
import com.yonyou.pmclouds.safetypatrol.process.SafetypatrolUpdateCheckProcess;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolQuery;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolService;
import org.apache.commons.lang.ArrayUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徐老板
 * @date 2020/4/16   18:53
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = SafetypatrolService.class)
public class SafetypatrolServiceImpl implements SafetypatrolService {

    @Resource
    private SafetypatrolMapper safetypatrolMapper;
    @Resource
    private SafetypatrolDetailMapper safetypatrolDetailMapper;
    @Resource
    private SafetypatrolQuery safetypatrolQuery;
    @Resource
    private PictureRemoteService pictureRemoteService;



    //首页创建按钮
    @Override
    public SafetypatrolVO insert(SafetypatrolVO safetypatrolVO) throws BusinessException {
        //插入是对主从表同时插入 所以需要两个模板
        SingleInsertBpTemplate<SafetypatrolVO> singleInsertBpTemplate = new SingleInsertBpTemplate(safetypatrolMapper);
        SingleInsertBpTemplate<SafetypatrolDetailVO> safetypatrolDetailVOSingleInsertBpTemplate = new SingleInsertBpTemplate<>(safetypatrolDetailMapper);
        //插入操作 首先检查数据库中有没有相同的检查标准 这里是某一企业的检查标准名称 类似hash
        singleInsertBpTemplate.addBeforeProcessor(new SafetypatrolInsertCheckProcess(safetypatrolMapper));
//        //补全参数
        singleInsertBpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<SafetypatrolVO>());
        safetypatrolDetailVOSingleInsertBpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<SafetypatrolDetailVO>());
        //执行插入
        singleInsertBpTemplate.insert(safetypatrolVO);
        //设置主从表关联关系 其实就是给每一个细节类的外键赋值 给细节类赋值 这里是从一个类分表
        if (safetypatrolVO.getSafeDetailVOS()!=null && safetypatrolVO.getSafeDetailVOS().length>0){
        for (SafetypatrolDetailVO safetypatrolDetailVO  : safetypatrolVO.getSafeDetailVOS()) {
            BeanHelper.setProperty(safetypatrolDetailVO, "pkSafetypatrol", safetypatrolVO.getPkSafetypatrol());
            //执行分表
            safetypatrolDetailVOSingleInsertBpTemplate.insert(safetypatrolDetailVO);
        }

        //添加事例照片  个数限制 编号设置
        insertExamplePics(safetypatrolVO);
        }

        return safetypatrolVO;
    }


    //通用按钮 插入示例图片
    @Override
    public SafetypatrolVO insertExamplePics(SafetypatrolVO safetypatrolVO) throws BusinessException {
        if (safetypatrolVO==null &&safetypatrolVO.getSafeDetailVOS()==null){
            return safetypatrolVO;
        }

        List<PictureVO> pictureVOList = setSimplePicture(safetypatrolVO);
        if (pictureVOList!=null &&pictureVOList.size()>0){
           pictureRemoteService.insertPictures(pictureVOList.toArray(new PictureVO[pictureVOList.size()]));
        }

        //防止别人修改数据
        return safetypatrolQuery.queryById(safetypatrolVO.getPkSafetypatrol());
    }

    //通用按钮 删除
    @Override
    public void delete(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleDeleteBpTemplate<SafetypatrolVO> safetypatrolVOSingleDeleteBpTemplate =new SingleDeleteBpTemplate<>(safetypatrolMapper);
        safetypatrolVOSingleDeleteBpTemplate.addBeforeProcessor(new SafetypatrolDeleteCheckProcess(safetypatrolMapper));
        safetypatrolVOSingleDeleteBpTemplate.delete(safetypatrolVO);
        //因为示例图片属于公共接口 这里再删除
        pictureRemoteService.deleteExamplePicsOfBill(safetypatrolVO.getPkSafetypatrol());
    }

    //首页编辑按钮 传入参数待和前端沟通
    @Override
    public SafetypatrolVO update(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleUpdateBpTemplate<SafetypatrolVO> safetypatrolVOSingleUpdateBpTemplate =new SingleUpdateBpTemplate<SafetypatrolVO>(safetypatrolMapper, new String[]{"standardName","standardType"});
        //细节类操作模板
        SingleUpdateBpTemplate<SafetypatrolDetailVO> detailVOSingleUpdateBpTemplate =new SingleUpdateBpTemplate<SafetypatrolDetailVO>(safetypatrolDetailMapper, new String[]{"checkname","checkdescription","needphoto"});
        SingleInsertBpTemplate<SafetypatrolDetailVO> detailVOSingleInsertBpTemplate = new SingleInsertBpTemplate<>(safetypatrolDetailMapper);
        SingleDeleteBpTemplate<SafetypatrolDetailVO> detailVOSingleDeleteBpTemplate = new SingleDeleteBpTemplate<>(safetypatrolDetailMapper);

        //主表添加公有字段
        safetypatrolVOSingleUpdateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
        //主表检验是否重命名
        safetypatrolVOSingleUpdateBpTemplate.addBeforeProcessor(new SafetypatrolUpdateCheckProcess(safetypatrolMapper) );
        //主表执行修改  这个问一下 更新操作不会更新changeflag吗
        safetypatrolVOSingleUpdateBpTemplate.update(safetypatrolVO);

        //从表更新前添加公有字段
        detailVOSingleUpdateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
        //从表插入前添加公有字段
        detailVOSingleInsertBpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<>());
        //从表删除前添加公有字段
        detailVOSingleDeleteBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());

        Map<String,String> map =new HashMap<>();

        //执行子表修改操作
        SafetypatrolDetailVO[] safeDetailVOS = safetypatrolVO.getSafeDetailVOS();
        if (safeDetailVOS!=null && safeDetailVOS.length>0){
            //传过来的数据 要么有要么没有 没有就新增 有就更改
            for (SafetypatrolDetailVO safeDetailVO : safeDetailVOS) {

                //这里是根据主键进行更新操作 所以这里要对主键进行非空校验
                if (safeDetailVO.getPkSafetypatroldetail()!=null && safeDetailVO.getPkSafetypatroldetail()!=""){
                    detailVOSingleUpdateBpTemplate.update(safeDetailVO);

                }
                else if(safeDetailVO.getPkSafetypatroldetail()==null || safeDetailVO.getPkSafetypatroldetail()==""){
                    safeDetailVO.setPkSafetypatrol(safetypatrolVO.getPkSafetypatrol());
                    detailVOSingleInsertBpTemplate.insert(safeDetailVO);

                }



                //接下来判断传入的数据有没有删除的数据 有就进行删除 这里最好让前端传入子表主键值 直接赋值就好
                //得到之前的细节值
                List<SafetypatrolDetailVO> orgDetailVos = safetypatrolDetailMapper.findById(safeDetailVO.getPkSafetypatrol());
                //得到传入的细节值
                SafetypatrolDetailVO[] nowDetailVos = safetypatrolVO.getSafeDetailVOS();
                //得到需要删除的细节id集合
                List<String> deletePkDetailList = needDeletePkDetailList(orgDetailVos, nowDetailVos);

                //执行删除细节操作
                for (String s : deletePkDetailList) {

                    //测试完成 OK
                   detailVOSingleDeleteBpTemplate.delete(safetypatrolDetailMapper.getById(s));
                }
//
                //更新示例图片 直接使用公共接口即可
                List<PictureVO> pictureVOList = setSimplePicture(safetypatrolVO);
//                注意这里需要的是数组
                pictureRemoteService.updateExamplePics(pictureVOList.toArray(new PictureVO[pictureVOList.size()]), safetypatrolVO.getPkSafetypatrol());

            }
        }

        //返回更新后的实体对象
        return safetypatrolQuery.queryById(safetypatrolVO.getPkSafetypatrol());
    }


    //首页的启用按钮
    @Override
    public SafetypatrolVO enable(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleUpdateBpTemplate<SafetypatrolVO> updateBpTemplate =new SingleUpdateBpTemplate(safetypatrolMapper,new String[]{"stateCode"} );
        if(safetypatrolVO!=null ){
            updateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
            safetypatrolVO.setStateCode(ParentState.ENABLE);
            SafetypatrolVO update = updateBpTemplate.update(safetypatrolVO);
            System.out.println(update);
            return update;
        }
      return null;
    }

    //首页的停用按钮
    @Override
    public SafetypatrolVO disable(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleUpdateBpTemplate<SafetypatrolVO> updateBpTemplate =new SingleUpdateBpTemplate(safetypatrolMapper,new String[]{"stateCode"} );
        if(safetypatrolVO!=null ){
            updateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
            safetypatrolVO.setStateCode(ParentState.DISABLE);
            //通过这个方法返回 不是直接返回
            return updateBpTemplate.update(safetypatrolVO);

        }
        return null;
    }

    //通用方法 查看数据是否被引用
    @Override
    public boolean referCheck(SafetypatrolVO safetypatrolVO) throws BusinessException {
        Integer count = safetypatrolMapper.checkQupte(safetypatrolVO);
        if (count!=null && count>0){
//            throw  new BusinessException("安全标准已经被引用，不允许修改!", BuziExCodeConst.CODE_NAME_BEEN_USED);
            return true;
        }

        return false;

    }

    //service层方法 获得需要删除的主键
    private List<String> needDeletePkDetailList(List<SafetypatrolDetailVO> orgDetailVos, SafetypatrolDetailVO[] nowDetailVos) {
        List<String> needList =new ArrayList<>();
        if (orgDetailVos ==null && orgDetailVos.size()==0){
            //对之前数据进行非空校验 若为空则有问题 无法进行删除操作
            return needList;
        }
        //把传过来的主键放到list集合中作为比较条件
        List<String> compareList=new ArrayList<>();
        for (SafetypatrolDetailVO nowDetailCompare : nowDetailVos) {
            compareList.add(nowDetailCompare.getPkSafetypatroldetail());
        }

        System.out.println(compareList);

        for (SafetypatrolDetailVO orgDetailCompare : orgDetailVos) {

            System.out.println(orgDetailCompare.getPkSafetypatroldetail());

            if (!compareList.contains(orgDetailCompare.getPkSafetypatroldetail())){
                needList.add(orgDetailCompare.getPkSafetypatroldetail());
            }
        }
        System.out.println(needList);

        return needList;


    }



    //service层方法 设置示例图片方法
    List<PictureVO> setSimplePicture(SafetypatrolVO safetypatrolVO) throws BusinessException {
        List<PictureVO> pictureVOList =new ArrayList<>();
        SafetypatrolDetailVO[] safeDetailVOS = safetypatrolVO.getSafeDetailVOS();

        for (SafetypatrolDetailVO safetypatrolDetailVO : safeDetailVOS) {
            if (ArrayUtils.isEmpty(safetypatrolDetailVO.getExamplePictures()) ){
                continue;
            }
            if (safetypatrolDetailVO.getExamplePictures().length>5){
                throw new BusinessException("示例图片数量不能超过5张");
            }
            int i =1;
            //给分离出的图片赋值编号 设置示例图片两个关联外键 设置顺序 设置exampleflag
            for (PictureVO pictureVO : safetypatrolDetailVO.getExamplePictures()) {
                pictureVO.setSequence(i);
                i++;
                pictureVO.setExampleFlag(1);
                pictureVO.setPk_entity(safetypatrolDetailVO.getPkSafetypatroldetail());
                pictureVO.setPk_bill(safetypatrolVO.getPkSafetypatrol());
                pictureVOList.add(pictureVO);
            }
        }

        return pictureVOList;
    }
}
